export default {
  $getHeight(selector) {
    return new Promise((resolve, reject) => {
      wx.createSelectorQuery().select(selector).boundingClientRect().exec((res) => {
        if (res[0]) {
          resolve(res[0]);
        } else {
          reject(`未找到指定的元素：${selector}`);
        }
      });
    });
  },

  $getNode(selector, additionalProperties = []) {
    const properties = ["margin", "backgroundColor", "fontSize", "color", "padding", ...additionalProperties];
    return new Promise((resolve) => {
      wx.createSelectorQuery().select(selector)
          .fields({ dataset: true, size: true, scrollOffset: true, properties: ["scrollX", "scrollY"], computedStyle: properties, context: true, node: true }, resolve)
          .exec();
    });
  },

  $getWxImg(src) {
    return new Promise((resolve, reject) => {
      wx.getImageInfo({
        src,
        success: resolve,
        fail: () => reject(`图片加载失败：${src}`)
      });
    });
  },

  getNav(that, id = "#Navigation") {
    const navigation = that.selectComponent(id);
    if (!navigation) {
      console.warn("未找到自定义导航组件，请确认组件ID是否正确。");
      return false;
    }
    // 根据需要处理导航组件
  },

  isIphoneX(that, otherHeight = 0, addDOM = true) {
    return new Promise((resolve) => {
      const nav = that.selectComponent("#Navigation");
      const menuInfo = wx.getMenuButtonBoundingClientRect();
      const res = wx.getSystemInfoSync(); // 使用同步方法获取系统信息

      const isIphone = res.model.startsWith("iPhone");
      const isIPX = isIphone && !["iPhone 4", "iPhone 5", "iPhone 6", "iPhone 7", "iPhone 8"].some(model => res.model.includes(model));
      let scrollHeight = res.windowHeight - otherHeight - (isIPX ? 34 : 0);

      let navHeight = nav ? (isIphone ? 44 : 48) + res.statusBarHeight : 0;
      if (that.data.haveNav) {
        navHeight = (isIphone ? 44 : 48) + res.statusBarHeight;
      }

      const navMenuHeight = menuInfo.height + (menuInfo.top - res.statusBarHeight) * 2;
      let newScrollHeight = scrollHeight - navMenuHeight - res.statusBarHeight;

      const leftMenuInfo = {
        left: res.windowWidth - menuInfo.right,
        width: menuInfo.width,
        height: menuInfo.height,
        top: menuInfo.top - res.statusBarHeight
      };

      const safeArea = {
        top: res.safeArea.top,
        height: res.safeArea.height,
        width: res.safeArea.width
      };

      const abSafeArea = `position:absolute;top:${safeArea.top}px;left:0;height:${safeArea.height}px;width:${safeArea.width}px`;
      const reSafeArea = `position:relative;padding-top:${safeArea.top}px;height:${safeArea.height}px;width:${safeArea.width}px`;

      const aniData = {
        isIPX,
        isIphone,
        windowHeight: res.windowHeight,
        windowWidth: res.windowWidth,
        scrollHeight,
        newScrollHeight,
        navHeight,
        navMenuHeight,
        navMenuAllHeight: navMenuHeight + res.statusBarHeight,
        navTop: res.statusBarHeight,
        leftMenuInfo,
        scale: (res.windowHeight / res.windowWidth).toFixed(4),
        menuInfo,
        area: { safeArea, abSafeArea, reSafeArea }
      };

      if (addDOM) {
        that.setData({ aniData });
      }

      resolve(aniData);
    });
  }

};
